package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.services.infrastructure.bean.ResourceBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GroupResourceMapper;
import com.molichuxing.services.infrastructure.dao.ResourceMapper;
import com.molichuxing.services.infrastructure.dto.response.response.GroupResourceDto;
import com.molichuxing.services.infrastructure.dto.response.response.ResourceDto;
import com.molichuxing.services.infrastructure.service.GroupResourceService;
import com.molichuxing.services.infrastructure.service.ResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {
    private static final Logger logger = LoggerFactory.getLogger(ResourceServiceImpl.class);

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public List<ResourceDto> getResoureTree(int systemType) {
        return editorTree(0, systemType);
    }

    @Override
    public List<ResourceDto> getTabList(int pid, int systemType, Integer groupId) {
        return Convert.toResourceDtoList(resourceMapper.getTabList(pid, systemType, groupId));
    }

    @Override
    public List<ResourceDto> getResourceListByParam(Map<String, Object> param) {
        return Convert.toResourceDtoList(resourceMapper.getResourceListByParam(param));
    }

    @Override
    public List<ResourceDto> getResourceByUri(String uri) {
        return  Convert.toResourceDtoList(resourceMapper.getResourceByUri(uri));
    }

    @Override
    public List<ResourceDto> getResourceTabByUri(String uri, int systemType, int groupId) {
        return  Convert.toResourceDtoList(resourceMapper.getResourceTabByUri(uri, systemType, groupId));
    }

    private List<ResourceDto> editorTree(int pid, int systemType) {
        List<ResourceDto> resourceDtoList = Convert.toResourceDtoList(resourceMapper.getResourceListByPid(pid, systemType));
        if (CollectionUtils.isNotEmpty(resourceDtoList)) {
            resourceDtoList.forEach(dto -> {
                dto.setChildren(editorTree(dto.getPermissionId(), systemType));
            });
        }
        return resourceDtoList;
    }
}
